package sg.edu.nus.iss.awsdynamodb;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sg.edu.nus.iss.CONFIG;
import sg.edu.nus.iss.stockex.StockCode;

import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodb.model.DeleteItemRequest;
import com.amazonaws.services.dynamodb.model.DeleteItemResult;
import com.amazonaws.services.dynamodb.model.Key;
import com.amazonaws.services.dynamodb.model.PutItemRequest;
import com.amazonaws.services.dynamodb.model.PutItemResult;
import com.amazonaws.services.dynamodb.model.ScanRequest;
import com.amazonaws.services.dynamodb.model.ScanResult;
public class StockCodeTable implements DynamoDBTableInf<StockCode> {
	
	private AmazonDynamoDBClient dynamoDB = DynamoDB.client();
	@Override
	
	public Map<String, AttributeValue> newItem(StockCode code) {
		Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
		item.put("code", new AttributeValue(code.getCode()));
		item.put("name", new AttributeValue(code.getName()));
		return item;
	}

	@Override
	public boolean Add(StockCode item) {
		 boolean status = false;
		 try{
		 Map<String,AttributeValue> map = newItem(item);
		 PutItemRequest putItemRequest = new PutItemRequest(CONFIG.STOCK_CODE_TABLE_NAME, map);
		 PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
		 System.out.println("Result: " + putItemResult);
		 if(putItemResult!=null)
		 {
			 status =  true;
		 }
		 }catch (Exception e) {
			 e.printStackTrace();
			// TODO: handle exception
		}
		
		 return status;
	}

	@Override
	public boolean Delete(StockCode item) {
		
		boolean status = false;
		try{
			DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName(CONFIG.STOCK_CODE_TABLE_NAME).withKey(new Key().withHashKeyElement(new AttributeValue(item.getCode())));
			DeleteItemResult result = dynamoDB.deleteItem(deleteItemRequest);
			if(result!=null)
			{
				status = true;	
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return status;
		
	}
	
	public List<String> getCodes(){
		
		List<String> codeList = new ArrayList<String>();
		List<StockCode> stockCodeList = getStockCodes();
		for (StockCode stockCode : stockCodeList)
			codeList.add(stockCode.getCode());
		return codeList;
	}
	
public List<String> getCodesToDisplay(){
		
		List<String> displayList = new ArrayList<String>();
		List<StockCode> stockCodeList = getStockCodes();
		for (StockCode stockCode : stockCodeList)
			displayList.add(stockCode.getName() + " [" + stockCode.getCode()+"]");
		Collections.sort(displayList);
		return displayList;
	}
	
	public List<StockCode> getStockCodes()
	{
		//DynamoDB.waitForTableToBecomeAvailable(CONFIG.STOCK_CODE_TABLE_NAME);
    	List<StockCode> codeList = new ArrayList<StockCode>(); 
        ScanRequest scanRequest = new ScanRequest(CONFIG.STOCK_CODE_TABLE_NAME);
        ScanResult scanResult = dynamoDB.scan(scanRequest);
        List<Map<String,AttributeValue>> CodeMapList  = scanResult.getItems();
        System.out.println("CodeMapList:"+CodeMapList.size());

        System.out.println("Displaying all the data....");
        for (Map<String, AttributeValue> map : CodeMapList) {
        	StockCode stockCode = convertMapToCode(map);
        
        	if(stockCode!=null)
        	{
        		codeList.add(stockCode);
        	}
		}
        return codeList;
	}

	 public StockCode convertMapToCode(Map<String, AttributeValue> map)
	    {
	    	StockCode stockCode = null;
	    	try {
	    	 String code = map.get("code").getS();
	    	 String name = map.get("name").getS();
	    	 
	    	 stockCode = new StockCode(code,name);
			} catch (Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
				return null;
				// TODO: handle exception
			}
	    	return stockCode;
	    }
	    
	
	public  void createTable() {
		DynamoDB.createTable(CONFIG.STOCK_CODE_TABLE_NAME, CONFIG.STOCK_CODE_PRIMARY_KEY, CONFIG.STOCK_CODE_PRIMARY_KEY_TYPE,CONFIG.STOCK_CODE_RANGE_KEY,CONFIG.STOCK_CODE_RANGE_KEY_TYPE);
	}

}
